﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PagedList;
using System.Web.Mvc;
using PTUDW_CNTN2010_F_CDT.Models;
using System.Diagnostics;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    public class AdminAccountController : Controller
    {
        EcommerceDBContext db = new EcommerceDBContext();

        public ActionResult ViewAccounts()
        {
            return View();
        }

        public JsonResult GetListUsers(int? page, int? rows, string sidx, string sord)
        {
            var assets = from c in db.Customers
                         orderby c.UserName
                         select new
                         {
                             ID = c.CustomerId,
                             UserName = c.UserName,
                             Password = c.Password,
                             Email = c.Email,
                             FullName = c.FullName,
                             Genre = c.Sex,
                             Address = c.Address,
                             Phone = c.Phone,
                             IsActive = c.IsActive,
                             Level = c.Level
                         };

            //var sortedAssets = assets.SortBy(sidx, sord.Contains("DESC"));
            var pagedAssets = assets.ToPagedList(page ?? 1, rows ?? 20);

            var results = new
            {
                total = pagedAssets.PageCount, //number of pages
                page = pagedAssets.PageNumber, //current page
                records = assets.Count(), //total items
                rows = pagedAssets
            };

            return Json(results);
        }

        public void Delete(int ID)
        {
            var cus = db.Customers.Where(c => c.CustomerId == ID);
            db.Customers.Remove(cus.ToList()[0]);
            db.SaveChanges();
        }

        [HttpPost]
        public void SaveChange(int ID, string UserName, string Password, string Email, string FullName, string Genre, string Address, string Phone, string IsActive, int Level)
        {
            var customer = db.Customers.Find(UserName);
            if (customer == null)
            {
                customer = new Customer();
                db.Customers.Add(customer);
                customer.ProvinceId = 1;
                customer.IsActive = false;
                customer.IsAdmin = false;
            }
            customer.UserName = UserName;
            customer.Password = Password;
            customer.Email = Email;
            customer.FullName = FullName;
            customer.Sex = (Genre == "false");
            customer.Address = Address;
            customer.Phone = Phone;
            customer.Level = Level;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
        }
    }
}
